package com.fenghuajueli.module_home.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fenghuajueli.module_home.bean.HomeToolsBean;
import com.fenghuajueli.module_home.databinding.ItemHomeToolsBinding;

import java.util.List;

public class HomeToolsAdapter extends RecyclerView.Adapter<HomeToolsAdapter.MyViewHolder> {

    List<HomeToolsBean> homeToolsBeans;

    public HomeToolsAdapter(List<HomeToolsBean> homeToolsBeans) {
        this.homeToolsBeans = homeToolsBeans;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemHomeToolsBinding binding = ItemHomeToolsBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        HomeToolsBean homeToolsBean = homeToolsBeans.get(position);
        holder.binding.ivRes.setImageResource(homeToolsBean.res);
        holder.binding.tvName.setText(homeToolsBean.name);
        holder.binding.tvDesc.setText(homeToolsBean.desc);
        holder.binding.container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(homeToolsBean.name);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return homeToolsBeans.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final ItemHomeToolsBinding binding;

        public MyViewHolder(@NonNull ItemHomeToolsBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(String name);
    }

    OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
